package com.climber.spb_rabbitmq.spb_confirm_return.utils;

import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

@Component
public class MyCallBack implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnsCallback {
    /**
     * 交换机不管是否收到消息的一个回调方法
     * CorrelationData 消息相关数据
     * ack 交换机是否收到消息
     */
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause)
    {String id=correlationData!=null?correlationData.getId():"";
        if(ack){
            System.out.printf("交换机已经收到 id 为:%s的消息\n",id);
        }else{
            System.out.printf("交换机还未收到 id 为:%s消息,由于原因:%s\n",id,cause);
        }
    }

    @Override
    public void returnedMessage(ReturnedMessage returned) {
        System.out.printf("回退消息%s\n",returned);
    }

}
